package com.konne.nightmare.FastPublicOpinion.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.konne.nightmare.FastPublicOpinion.Event;
import com.konne.nightmare.FastPublicOpinion.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Create by ws.
 * on Data:2022/2/12  Time:下午 1:07.
 * description: 从课程导入 .
 */
public class CourseView extends FrameLayout {
    private RecyclerView recyclerView;
    public CourseView(@NonNull Context context) {
        this(context,null);
    }

    public CourseView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CourseView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        recyclerView=new RecyclerView(context);
        addView(recyclerView,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        setCourseData(getCourse());
    }
    private void addCourseInformation(Course course){
        View view= LayoutInflater.from(getContext()).inflate(R.layout.view_course_information,null);
        addView(view,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        TextView tv_title=view.findViewById(R.id.tv_title);
        tv_title.setText(String.format("课程 > %s",course.title));
        tv_title.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeView(view);
            }
        });
        CourseInfoAdapter courseInfoAdapter=new CourseInfoAdapter(getInfos());
        view.findViewById(R.id.stv_submit).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //这里选择的数据可以通过Event传递或者其他方式回调
                if(courseInfoAdapter.selInfo!=null){
                    EventBus.getDefault().post(new Event.AddDataItem());
                }
                ActivityUtils.getTopActivity().finish();
            }
        });
        RecyclerView recyclerView=view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(courseInfoAdapter);
    }


    public void setCourseData(List<Course> datas){
        recyclerView.setAdapter(new CourseAdapter(datas));
    }

    //模拟获取答疑数据
    private List<Info> getInfos(){
        List<Info> datas=new ArrayList<>();
        datas.add(new Info("13.1答疑","21-3-21 12:23"));
        datas.add(new Info("13.1答疑","21-3-21 12:23"));
        datas.add(new Info("13.1答疑","21-3-21 12:23"));
        datas.add(new Info("13.1答疑","21-3-21 12:23"));
        return datas;
    }
    //模拟获取课程数据
    private List<Course> getCourse(){
        List<Course> datas=new ArrayList<>();
        datas.add(new Course("https://img1.baidu.com/it/u=2201498406,3778061117&fm=253&fmt=auto&app=138&f=JPEG?w=498&h=500"
                ,"高等数学","数学五班"));
        datas.add(new Course("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F30%2F90%2F40%2F309040a0602c672cebc6ab3a1bbbc8cd.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647235144&t=26b29ba1d6212d3723b673e08ad7b637"
                ,"高等数学","数学五班"));
        datas.add(new Course("https://img1.baidu.com/it/u=2201498406,3778061117&fm=253&fmt=auto&app=138&f=JPEG?w=498&h=500"
                ,"高等数学","数学五班"));
        datas.add(new Course("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F30%2F90%2F40%2F309040a0602c672cebc6ab3a1bbbc8cd.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647235144&t=26b29ba1d6212d3723b673e08ad7b637"
                ,"高等数学","数学五班"));
        return datas;
    }

    class CourseInfoAdapter extends BaseQuickAdapter<Info,BaseViewHolder>{
        public Info selInfo;
        public CourseInfoAdapter(@Nullable List<Info> data) {
            super(R.layout.item_course_infor,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Info item) {
            helper.setText(R.id.tv_title,item.title)
                    .setText(R.id.tv_time,item.time);
            QJCheckBox checkBox=helper.getView(R.id.checkbox);
            checkBox.setChecked(item==selInfo);
            checkBox.setOnCheckedChangeListener(new QJCheckBox.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(QJCheckBox var1, boolean var2) {
                    //item.isSel=var2;
                    if(var2){
                        selInfo=item;
                    }else {
                        selInfo=null;
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }
    class CourseAdapter extends BaseQuickAdapter<Course, BaseViewHolder>{
        public CourseAdapter(@Nullable List<Course> data) {
            super(R.layout.item_course,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Course item) {
            ImageView iv_pic=helper.getView(R.id.iv_pic);
            Glide.with(iv_pic)
                    .load(item.picUrl)
                    .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(SizeUtils.dp2px(12),0)))
                    .into(iv_pic);
            helper.setText(R.id.tv_title,item.title)
                    .setText(R.id.tv_sub_title,item.className);
            helper.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    addCourseInformation(item);
                }
            });
        }
    }
    class Info{
        String title;
        String time;
        boolean isSel;
        public Info(String title, String time) {
            this.title = title;
            this.time = time;
        }
    }
    class Course{
        String picUrl;
        String title;
        String className;

        public Course(String picUrl, String title, String className) {
            this.picUrl = picUrl;
            this.title = title;
            this.className = className;
        }
    }
}
